/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.swan.plugin.util;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.ResourceLoader;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.core.resource.FileResourceLoader;
import org.swan.plugin.beetl.CodeFunction;
import org.swan.plugin.beetl.CollectionFunction;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class Template {
    /**
     * Used to separate file resource to root and resource
     */
    public static final String SEPARATE = "@@";
    private Map<String, Object> variables = new HashMap<>();

    private String resource;

    public boolean generateFileTemplateTo(File outputFile) throws IOException {
        return to(outputFile, false);
    }

    public boolean generateResourceTemplateTo(File outputFile) throws IOException {
        return to(outputFile, true);
    }

    private boolean to(File outputFile, boolean isResource) throws IOException {
        Configuration configuration = Configuration.defaultConfiguration();
        ResourceLoader<String> resourceLoader = isResource ? new ClasspathResourceLoader() : new FileResourceLoader(getRoot());
        GroupTemplate groupTemplate = new GroupTemplate(resourceLoader, configuration);
        groupTemplate.registerFunctionPackage("code", CodeFunction.class);
        groupTemplate.registerFunctionPackage("listUtil", CollectionFunction.class);
        final org.beetl.core.Template template1 = groupTemplate.getTemplate(getResource());
        for (Map.Entry<String, Object> entry : variables.entrySet()) {
            template1.binding(entry.getKey(), entry.getValue());
        }
        try (FileWriter fileWriter = new FileWriter(outputFile)) {
            template1.renderTo(fileWriter);
        }
        return true;
    }

    public Object getVariable(String key) {
        return variables.get(key);
    }

    public Template putVariable(String key, Object value) {
        variables.put(key, value);
        return this;
    }

    public void clearVariable() {
        variables.clear();
    }

    public String getResource() {
        if (resource == null)
            return null;
        int pos = resource.indexOf(SEPARATE);
        if (pos == -1)
            return resource;
        return resource.substring(pos + SEPARATE.length());
    }

    public String getRoot() {
        if (resource == null)
            return null;
        int pos = resource.indexOf(SEPARATE);
        if (pos == -1)
            return null;
        return resource.substring(0, pos);
    }

    public Template setResource(String resource) {
        this.resource = resource;
        return this;
    }

}
